<template>
    <div>
        1111
        <button @click="register">注册</button>
        <button @click="login">登录</button>
        <button @click="getAppkeyChatrooms">获取聊天室列表</button>
        <p>聊天室Id</p>
        <input type="text" v-model="chatroomId" />
        <button @click="enterChatroom">加入聊天室</button>
        <button @click="exitChatroom ">退出聊天室</button>
        <p>发送内容</p>
        <input type="text" v-model="chatMsg"/>
        <button @click="sendChatroomMsg">发送聊天室消息</button>
        <input type="text" v-model="chatCustomMsg"/>
        <button @click="sendChatroomCustom">发送聊天室自定义消息</button>
    </div>
</template>
<script>
var JIM = new JMessage();
export default {
    data(){
        return {
            chatroomId: "",
            chatMsg: "",
            chatCustomMsg: "",
        }
    },
    mounted(){
        this.getImAuthPayload();
    },
    methods: {
        getImAuthPayload(){
            this.$http({
                url: "/api/jmsgAuth",
                method: "get"
            }).then(res=>{
                JIM.init({
                    "appkey": res.data.appkey,
                    "random_str": res.data.random_str,
                    "signature": res.data.signature,
                    "timestamp": res.data.timestamp,
                    // "appkey": "08c6812a0c062e68ea2e3230",
                    // "random_str": "0f1837fa005144308746da6a6f7de26d",
                    // "signature": "bad71016f84d1572cc8dc34ca6f159d4",
                    // "timestamp": "1582082344",
                    "flag" : 1
                }).onSuccess(function(data) {
                    // console.log("Success", data)
                    //data.code 返回码
                    //data.message 描述
                }).onFail(function(data) {
                    // console.log("Fail", data)
                    // 同上
                });
            });
        },
        register(){
            JIM.register({
                "username": "1b1f843dfa3549768a2d3d45ebbdb7c1",
                "password": "99bab5b0677f4c65a32298b796011c10",
                "is_md5": false,
                // "extras": {"unionid":"oS_NluHGq-zIqYM3dufD9tm2V4aw","img":"http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLOT8FHaH8mQg6PPSUicjG6OalibFicxP9Qf2uoboMiaMz4nAplUo4yHicwNXPqmgIUq2Xm4iazRNTbyaMw/132"},
                "address": "深圳"
            }).onSuccess(function(data) {
                console.log("registerSuccess", data)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("registerFail", data)
                // 同上
            });
        },
        login(){
            JIM.login({
                "username": "1b1f843dfa3549768a2d3d45ebbdb7c1",
                "password": "99bab5b0677f4c65a32298b796011c10",
                // "is_md5": true,
            }).onSuccess(function(data) {
                console.log("loginSuccess", data)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("loginFail", data)
                // 同上
            });
        },
        // 获取appkey下聊天室分页列表
        getAppkeyChatrooms(){
            JIM.getAppkeyChatrooms({
                "start": 0,
            }).onSuccess(function(data) {
                console.log("getAppkeyChatroomsSuccess", data)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("getAppkeyChatroomsFail", data)
                // 同上
            });
        },
        // 进入聊天室
        enterChatroom(){
            JIM.enterChatroom({
                "id": this.chatroomId,
            }).onSuccess(function(data) {
                console.log("enterChatroomSuccess", data)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("enterChatroomFail", data)
                // 同上
            });
            // 聊天室消息监听
            JIM.onRoomMsg(function(data) {
                console.log("聊天室消息监听", data)
                // data.room_id 聊天室 id
                // data.msg_id 消息 id
                // data.ctime_ms 消息生成时间
                // data.content
            });
        },
        // 
        exitChatroom(){
            JIM.exitChatroom({
                "id": this.chatroomId,
            }).onSuccess(function(data) {
                console.log("exitChatroomSuccess", data)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("exitChatroomFail", data)
                // 同上
            });
        },
        // 聊天室发送文本消息
        sendChatroomMsg(){
            JIM.sendChatroomMsg({
                "target_rid": this.chatroomId,
                // "target_rid": "23747459",
                "content": this.chatMsg,
                // "extras": "enter",
            }).onSuccess(function(data, msg) {
                console.log("sendChatroomMsgSuccess", data, msg)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("sendChatroomMsgFail", data)
                // 同上
            });
        },
        // 聊天室发送自定义消息
        sendChatroomCustom(){
                console.log("sendChatroomCustom", this.chatCustomMsg)
            JIM.sendChatroomCustom({
                "target_rid": this.chatroomId,
                // "target_rid": "23747459",
                "custom": {"custom":this.chatCustomMsg},
                // "extras": "enter",
            }).onSuccess(function(data, msg) {
                console.log("sendChatroomCustomSuccess", data, msg)
                //data.code 返回码
                //data.message 描述
            }).onFail(function(data) {
                console.log("sendChatroomCustomFail", data)
                // 同上
            });
        },
        
    },
}
</script>
<style lang="scss" scoped>
input{
    border: 1px solid #000;
}
</style>